<?php

namespace App\Http\Controllers\Api;

use App\Place;
use App\Schedule;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PlaceController extends Controller
{
    public function __construct()
    {
        $this->middleware(function (Request $request,$next){
            $role = User::whereToken($request->token)->pluck('role')->first();
            if (!$role){
                return response()->json('Unauthorized user',401);
            }
            return $next($request);
        })->only('store','update','destroy');
        $this->middleware(function (Request $request,$next){
            $user = User::whereToken($request->token)->first();
            if (!$user){
                return response()->json('Unauthorized user',401);
            }
            return $next($request);
        })->only('index','show');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $place = Place::all();
        return response()->json($place,200);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        if (!$request->has('name','latitude','longitude','x','y') || !$request->hasFile('image')){
            return response()->json('Data cannot be processed',422);
        }
        $data = $request->except('image','token');
        $image_path = str_random(20).'.jpeg';
        $request->image->storeAs('img',$image_path);
        if ($request->image->isValid()){
            $data['image_path'] = $image_path;
        }else{
            return response()->json('Data cannot be processed',422);
        }
        $place = Place::create($data);
        if ($place){
            return response()->json('crate success',200);
        }else{
            return response()->json('Data cannot be processed',422);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $place = Place::find($id);
        $place->numSearches = $place->numSearches+1;
        $place->save();
        return response()->json($place,200);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->except('image','token','_method');
        if ($request->hasFile('image')){
            $image_path = str_random(20).'.jpeg';
            $request->image->storeAs('img',$image_path);
            if ($request->image->isValid()){
                $data['image_path'] = $image_path;
            }else{
                return response()->json('Data cannot be processed',422);
            }
        }
        $place = Place::whereId($id)->update($data);
        if ($place){
            return response()->json('update success',200);
        }else{
            return response()->json('Data cannot be updated',422);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $shc = Schedule::where('from_place_id',$id)->orwhere('to_place_id',$id)->first();
        if($shc){
            return response()->json('Data cannot be deleted',400);
        }
        $place = Place::whereId($id)->delete();
        if ($place){
            return response()->json('delete success',200);
        }else{
            return response()->json('Data cannot be deleted',400);
        }
    }
}
